package com.color.admin.system.entity;


import com.color.admin.common.core.constant.Constant;
import com.color.admin.common.core.constant.ConstantData;
import io.swagger.v3.oas.annotations.media.Schema;
import org.hibernate.annotations.GenericGenerator;

import javax.persistence.*;
import javax.validation.constraints.*;
import java.util.Set;

/**
 * @author HaiQiu
 * @date 2021/12/6
 */
@Entity
@Table(name = ConstantData.USER_TABLE, schema = ConstantData.SOURCE)
public class SysUser extends BaseEntity{

    @NotEmpty(groups = update.class,message = "ID"+ Constant.NOT_EMPTY)
    @Schema(example = "xx", description = "主键ID")
    private String id;

    @NotEmpty(message = "用户名"+Constant.NOT_EMPTY)
    @Pattern(regexp = "^[a-z0-9_-]{3,15}$",message = "用户名只能是数字或者英文或组合")
    @Size(max = 20,message = "用户名长度只能在1-20个字符")
    @Schema(example = "test",description = "用户名",required = true)
    private String username;

    @Size(max = 50,message = "昵称长度只能在1-50个字符")
    @Schema(example = "赠她鱼香",description = "昵称")
    private String nickname;

    @Size(max = 50,message = "姓名长度只能在1-50个字符")
    @Schema(example = "海棠",description = "真实姓名")
    private String realName;

    @NotEmpty(groups = add.class,message = "密码"+Constant.NOT_EMPTY)
    @Size(max = 100,message = "密码长度只能在1-100个字符")
    @Schema(example = "123123",description = "密码")
    private String password;

    @Size(max = 20,message = "手机长度只能在1-20个字符")
    @Pattern(regexp = "0?(13|14|15|18)[0-9]{9}",message = "手机号码不正确")
    @Schema(example = "15111221122", description = "手机")
    private String phone;

    @Size(max = 800,message = "头像地址只能在1-800个字符")
    @Schema(example = "https://gw.alipayobjects.com/zos/rmsportal/jZUIxmJycoymBprLOUbT.png",
            type = "string", description = "头像")
    private String avatar;

    @Size(max = 100,message = "邮箱长度只能在1-100个字符")
    @Email(message = "邮箱格式不正确")
    @Schema(example = "test@qq.com", description = "邮箱")
    private String email;

    @Size(max = 20,message = "生日长度只能在1-20个字符")
    @Schema(example = "2000-10-01", description = "生日")
    private String birthday;

    @Size(max = 200,message = "签名长度只能在1-200个字符")
    @Schema(example = "关于我", description = "签名||关于")
    private String about;

    @Size(max = 50,message = "地区长度只能在1-50个字符")
    @Schema(example = "中国-贵州-遵义",description = "地区")
    private String location;


    @Min(value = 0,message = "性别参数值应该在0-2之间")
    @Max(value = 2,message = "性别参数值应该在0-2之间")
    @Schema(example = "2", description = "性别：0男，1女，2保密")
    private Integer sex;

    @NotNull(message = "状态"+Constant.NOT_EMPTY)
    @Min(value = 0,message = "状态参数值应该在0-2之间")
    @Max(value = 2,message = "状态参数值应该在0-2之间")
    @Schema(description = "状态：0正常，1冻结")
    private Integer status;

    @Schema(description = "角色集合")
    private Set<SysRole> roles;

    @Schema(description = "部门对象")
    private SysDept dept;

    @Schema(description = "岗位集合")
    private Set<SysPost> posts;

    @Id
    @Column(name = "id")
    @GeneratedValue(generator = "id")
    @GenericGenerator(name = "id", strategy = "org.hibernate.id.UUIDGenerator")
    public String getId() {
        return id;
    }

    public void setId(String id) {
        this.id = id;
    }

    @Basic
    @Column(name = "username",length = 100)
    public String getUsername() {
        return username;
    }

    public void setUsername(String username) {
        this.username = username;
    }

    @Basic
    @Column(name = "password",length = 500)
    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    @Basic
    @Column(name = "nickname",length = 100)
    public String getNickname() {
        return nickname;
    }

    public void setNickname(String nickname) {
        this.nickname = nickname;
    }


    @Basic
    @Column(name = "real_name",length = 100)
    public String getRealName() {
        return realName;
    }

    public void setRealName(String realName) {
        this.realName = realName;
    }

    @Basic
    @Column(name = "phone",length = 20)
    public String getPhone() {
        return phone;
    }

    public void setPhone(String phone) {
        this.phone = phone;
    }

    @Basic
    @Column(name = "avatar",length = 800)
    public String getAvatar() {
        return avatar;
    }

    public void setAvatar(String avatar) {
        this.avatar = avatar;
    }

    @Basic
    @Column(name = "email",length = 100)
    public String getEmail() {
        return email;
    }

    public void setEmail(String email) {
        this.email = email;
    }

    @Basic
    @Column(name = "sex",length = 1)
    public Integer getSex() {
        return sex;
    }

    public void setSex(Integer sex) {
        this.sex = sex;
    }

    @Basic
    @Column(name = "status",length = 1)
    public Integer getStatus() {
        return status;
    }

    public void setStatus(Integer status) {
        this.status = status;
    }

    @ManyToMany(fetch = FetchType.EAGER)
    @JoinTable(name = ConstantData.USER_ROLE_TABLE,
            joinColumns = {@JoinColumn(name = "user_id")},
            inverseJoinColumns = {@JoinColumn(name = "role_id")})
    public Set<SysRole> getRoles() {
        return roles;
    }

    public void setRoles(Set<SysRole> roles) {
        this.roles = roles;
    }

    @ManyToOne
    @JoinTable(name = ConstantData.USER_DEPT_TABLE,
            joinColumns = {@JoinColumn(name = "user_id")},
            inverseJoinColumns = {@JoinColumn(name = "dept_id")})
    public SysDept getDept() {
        return dept;
    }

    public void setDept(SysDept dept) {
        this.dept = dept;
    }

    @ManyToMany(fetch = FetchType.EAGER)
    @JoinTable(name = ConstantData.USER_POST_TABLE,
            joinColumns = {@JoinColumn(name = "user_id")},
            inverseJoinColumns = {@JoinColumn(name = "post_id")})
    public Set<SysPost> getPosts() {
        return posts;
    }


    @Column(length = 20)
    public String getBirthday() {
        return birthday;
    }

    public void setBirthday(String birthday) {
        this.birthday = birthday;
    }

    @Column(length = 200)
    public String getAbout() {
        return about;
    }

    public void setAbout(String about) {
        this.about = about;
    }

    public void setPosts(Set<SysPost> posts) {
        this.posts = posts;
    }

    public String getLocation() {
        return location;
    }

    public void setLocation(String location) {
        this.location = location;
    }
}
